package com.wing.setting.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.setting.dao.SysSettingAdvertDao;
import com.wing.setting.model.entity.SysSetting;
import com.wing.setting.model.entity.SysSettingAdvert;
import com.wing.setting.model.request.SysSettingAdvertForm;
import com.wing.setting.model.response.SysSettingAdvertVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 刀具柜系统设置_广告设置-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SysSettingAdvertServiceImpl extends ServiceImpl<SysSettingAdvertDao, SysSettingAdvert> implements SysSettingAdvertService {

    @Lazy
    @Autowired
    private SysSettingService sysSettingService;

    @Override
    @Transactional
    public int add(SysSettingAdvertForm sysSettingAdvertForm) {

        //校验

        //查询广告主表信息
        SysSetting sysSetting = sysSettingService.getOne(sysSettingAdvertForm.getType(),JwtUtils.getEnterpriseCode());
        if (ObjectUtils.isEmpty(sysSetting)){
            throw new BusinessException("未查询到企业指定类型的广告主信息");
        }

        sysSettingAdvertForm.setSettingId(sysSetting.getId());
        sysSettingAdvertForm.setEnterpriseCode(JwtUtils.getEnterpriseCode());

        SysSettingAdvert sysSettingAdvert =new SysSettingAdvert();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SysSettingAdvert.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(sysSettingAdvertForm, sysSettingAdvert, copyOptions);

        if (baseMapper.insert(sysSettingAdvert)<=0){
            throw new BusinessException("广告图片设置添加失败");
        }

        return 1;
    }

    @Override
    public List<SysSettingAdvertVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SysSettingAdvertVO> pageList4app(Page page, Map<String, Object> params) {
        List<SysSettingAdvertVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SysSettingAdvertVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public boolean upIsDelete(Long id, Integer isDeleted) {
        SysSettingAdvert advert = this.getById(id);

        Boolean flag = null;
        if (isDeleted==0){
            flag = false;
        }else {
            flag = true;
        }
        advert.setIsDeleted(flag);
        if (this.updateById(advert)){
            return true;
        }
        return false;
    }

    @Override
    public List<SysSettingAdvertVO> getAdvertInfoBySetting(int type) {
        return baseMapper.getAdvertInfoBySetting(type);
    }

}
